/**
 * 
 */
package kr.co.insoft.auth.security.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
public class SuccessLoginHandler implements AuthenticationSuccessHandler {

	private static final Logger logger = LoggerFactory
			.getLogger(SuccessLoginHandler.class);

	final String firstPage;
	
	public SuccessLoginHandler(final String firstPage) {
		this.firstPage = firstPage;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.security.web.authentication.AuthenticationSuccessHandler
	 * #onAuthenticationSuccess(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse,
	 * org.springframework.security.core.Authentication)
	 */
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		try {
			response.sendRedirect(request.getContextPath() + firstPage);
		} catch (Exception e) {
			if ( logger.isDebugEnabled() ) {
				logger.debug("{}", e);
			}
			throw new IOException("Cannot obtain user information \n{}", e);
		}
	}
	
}
